package com.bzkj.web.controller.qualitymanagement;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bzkj.common.annotation.LogB;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.quality.domain.QmsCheckDTO;
import com.bzkj.quality.domain.QmsMaterialInfo;
import com.bzkj.quality.service.QmsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @projectName: bzkj-wms
 * @package: com.bzkj.web.controller.ncc
 * @className: QMSController
 * @description: 质量管理系统控制层
 * @author: C1309
 * @createDate: 2024年11月07日
 * @version: v1.0
 */
@RestController
@RequestMapping("/qms")
public class QmsController extends BaseController {

    /**
     * QMS服务层
     */
    @Resource
    QmsService qmsService;

    @GetMapping("/getBaseInfo")
//    @PreAuthorize("@ss.hasPermi('order:qms:query')")
    public AjaxResult getMaterialInfo(@RequestParam(name = "vbillcode") String billCode,
                                      @RequestParam("cgeneralbid") String cgeneralbid) {
        QmsMaterialInfo materialInfo = qmsService.getMaterialInfo(billCode, cgeneralbid, 0);
        materialInfo.setLoginUser(getLoginUser());
        return AjaxResult.success(materialInfo);
    }

    /**
     * wms推送数据到qms生成待办请验单
     */
    @PostMapping("/createQms")
    @PreAuthorize("@ss.hasPermi('order:qms:add')")
    @LogB(title = "请验管理", businessType = BusinessType.CHECK)
    public AjaxResult createQms(@RequestBody String responseBody) {
        JSONObject jsonObject = JSON.parseObject(responseBody);
        QmsCheckDTO qmsCheckDTO = jsonObject.getObject("detailsData", QmsCheckDTO.class);
        qmsCheckDTO.setCreateUserName(getLoginUser().getUser().getNickName());
        qmsCheckDTO.setCreateUserJobNum(getUsername());
        return qmsService.createQmsCheckInForm(qmsCheckDTO);
    }

    /**
     * 复验获取基础数据
     */
    @GetMapping("/re-inspection")
    public AjaxResult reInspection(@RequestParam("vbillcode") String vbillcode,
                                   @RequestParam("pk_material") String pkMaterial,
                                   @RequestParam("pk_batchcode") String pkBatchcode,
                                   @RequestParam("vbatchcode") String vbatchcode) {
        // 获取单据号、表头主键
        Map<String, String> maps = qmsService.reInspection(vbillcode, pkMaterial, pkBatchcode, vbatchcode);
        // 获取基础信息
        QmsMaterialInfo materialInfo = qmsService.getMaterialInfo(maps.get("vbillcode"), maps.get("cgeneralbid"), 1);
        // 向基础信息中插入当前登录用户信息
        materialInfo.setLoginUser(getLoginUser());
        return AjaxResult.success(materialInfo);
    }

    /**
     * 复验获取基础数据
     * @param id 复验ID
     * @return 基础信息
     */
    @GetMapping("re-inspection-chenchen")
    public AjaxResult reInspectionChenChen(@RequestParam("id") String id) {
        // 获取单据号、表头主键
       QmsMaterialInfo qmsMaterialInfo = qmsService.reInspectionChenChen(id);
       return AjaxResult.success(qmsMaterialInfo);
    }
}
